/**
 * Created by chenzhixin on 16/11/18.
 */

// import {i18n} from 'store'

/* 是否是公司邮箱*/
export function isWscnEmail(str) {
  const reg = /^[a-z0-9](?:[-_.+]?[a-z0-9]+)*@wallstreetcn\.com$/i;
  return reg.test(str.trim());
}

/* 合法uri*/
export function validateURL(textval) {
  const urlregex = /^(https?|ftp):\/\/([a-zA-Z0-9.-]+(:[a-zA-Z0-9.&%$-]+)*@)*((25[0-5]|2[0-4][0-9]|1[0-9]{2}|[1-9][0-9]?)(\.(25[0-5]|2[0-4][0-9]|1[0-9]{2}|[1-9]?[0-9])){3}|([a-zA-Z0-9-]+\.)*[a-zA-Z0-9-]+\.(com|edu|gov|int|mil|net|org|biz|arpa|info|name|pro|aero|coop|museum|[a-zA-Z]{2}))(:[0-9]+)*(\/($|[a-zA-Z0-9.,?'\\+&%$#=~_-]+))*$/;
  return urlregex.test(textval);
}

/* 小写字母*/
export function validateLowerCase(str) {
  const reg = /^[a-z]+$/;
  return reg.test(str);
}

/* 验证key*/
// export function validateKey(str) {
//     var reg = /^[a-z_\-:]+$/;
//     return reg.test(str);
// }
/* 正整数*/
export function validateNumCase(str) {
  const reg = /^\+?[1-9][0-9]*$/;
  return reg.test(str);
}
/* 大写字母*/
export function validateUpperCase(str) {
  const reg = /^[A-Z]+$/;
  return reg.test(str);
}

/* 大小写字母*/
export function validatAlphabets(str) {
  const reg = /^[A-Za-z]+$/;
  return reg.test(str);
}

/* 状态 0：异常，1：正常，2：未知*/
export function onlineStateFormat(value) {
  if (value === 0) {
    return "异常"
  }
  if (value === 1) {
    return "正常"
  }
  if (value === 2) {
    return "未知"
  }
}
/* "isActive": "integer,0：无效，1：有效"*/
export function isActiveFormat(value) {
  if (value === 0) {
    return "无效"
  }
  if (value === 1) {
    return "有效"
  }
}
export function numToWeek(value) {
  switch (value) {
    case 1:
      return '周日';
      break;
    case 2:
      return '周一';
      break;
    case 3:
      return '周二';
      break;
    case 4:
      return '周三';
      break;
    case 5:
      return '周四';
      break;
    case 6:
      return '周五';
      break;
    default:
      return '周六';
      break;
  }
}
export function weekToNum(value) {
  switch (value) {
    case '周日':
      return 1;
      break;
    case '周一':
      return 2;
      break;
    case '周二':
      return 3;
      break;
    case '周三':
      return 4;
      break;
    case '周四':
      return 5;
      break;
    case '周五':
      return 6;
      break;
    default:
      return '周六';
      break;
  }
}
export function numToTime(value) {
  value = value / 1000;
  let hour = parseInt(value / 3600);
  let min = parseInt((value - hour * 3600) / 60);
  let second = parseInt(value - hour * 3600 - min * 60);
  hour = hour > 9 ? hour : "0" + hour;
  min = min > 9 ? min : "0" + min;
  second = second > 9 ? second : "0" + second;
  if (arguments[1] == 1) return hour + ":" + min
  return hour + ":" + min + ":" + second;
}
/*获取时间格式数据*/
function _getTimeStampfunction() {
  var date = null;
  if (arguments[0] == "" || arguments[0] == null) {
    date = new Date();
  } else {
    date = new Date(arguments[0]);
  }
  var year = date.getFullYear();
  var month = date.getMonth() > 8 ? date.getMonth() + 1 : "0" + (date.getMonth() + 1);
  var day = date.getDate() > 9 ? date.getDate() : "0" + date.getDate();
  var hour = date.getHours() > 9 ? date.getHours() : "0" + date.getHours();
  var min = date.getMinutes() > 9 ? date.getMinutes() : "0" + date.getMinutes();
  var scd = date.getSeconds() > 9 ? date.getSeconds() : "0" + date.getSeconds();
  if (arguments[1] == 1) {
    return year + "-" + month + "-" + day;
  } else if (arguments[1] == 2) {
    return year + "年" + month + "月" + day + "日 ";
  } else if (arguments[1] == 3) {
    return year + "-" + month + "-" + day + " " + hour + ":" + min;
  } else if (arguments[1] == 4) {
    return year + "/" + month + "/" + day + " " + hour + ":" + min;
  } else if (arguments[1] == 5) {
    return hour + ":" + min + ":" + scd;
  } else if (arguments[1] == 6) {
    return hour + ":" + min;
  }else if (arguments[1] == 8) {
    return [hour,min,scd]
  }
  return year + "-" + month + "-" + day + " " + hour + ":" + min + ":" + scd;
}
/*排序*/

export function convertData(arr, type) {
  return arr.sort(function (a, b) {
    return a[type] - b[type]
  })
}
//从数组中寻找元素并删除元素 可指定多元素
export function destroyer(arr) {
  const newwArray = arguments[0];
  let removeArr = [];
  for (var i = 1, len = arguments.length; i < len; i++) {
    removeArr.push(arguments[i]);
  }
  function isFalse(value) {
    return removeArr.indexOf(value) === -1;
  }

  return newwArray.filter(isFalse);
}
//格局化日期：yyyy-MM-dd
function formatDate(date) {
  let year = date.getFullYear();
  let month = date.getMonth() > 8 ? date.getMonth() + 1 : "0" + (date.getMonth() + 1);
  let day = date.getDate() > 9 ? date.getDate() : "0" + date.getDate();
  let hour = date.getHours() > 9 ? date.getHours() : "0" + date.getHours();
  let min = date.getMinutes() > 9 ? date.getMinutes() : "0" + date.getMinutes();
  let scd = date.getSeconds() > 9 ? date.getSeconds() : "0" + date.getSeconds();
  return year + "-" + month + "-" + day + " " + hour + ":" + min + ":" + scd;
}
/**
 * 获取本周、本季度、本月、上月的开端日期、停止日期
 */
function getWeekOrMonth() {
  let now = new Date(); //当前日期
  let nowDayOfWeek = now.getDay(); //今天本周的第几天
  let nowDay = now.getDate(); //当前日
  let nowMonth = now.getMonth(); //当前月
  let nowYear = now.getYear(); //当前年
  nowYear += (nowYear < 2000) ? 1900 : 0; //

  let lastMonthDate = new Date(); //上月日期
  lastMonthDate.setDate(1);
  lastMonthDate.setMonth(lastMonthDate.getMonth() - 1);
  let lastYear = lastMonthDate.getYear();
  let lastMonth = lastMonthDate.getMonth();


//获得某月的天数
  function getMonthDays(myMonth) {
    let monthStartDate = new Date(nowYear, myMonth, 1);
    let monthEndDate = new Date(nowYear, myMonth + 1, 1);
    let days = (monthEndDate - monthStartDate) / (1000 * 60 * 60 * 24);
    return days;
  }

//获得本季度的开端月份
  function getQuarterStartMonth() {
    let quarterStartMonth = 0;
    if (nowMonth < 3) {
      quarterStartMonth = 0;
    }
    if (2 < nowMonth && nowMonth < 6) {
      quarterStartMonth = 3;
    }
    if (5 < nowMonth && nowMonth < 9) {
      quarterStartMonth = 6;
    }
    if (nowMonth > 8) {
      quarterStartMonth = 9;
    }
    return quarterStartMonth;
  }

//获得上周的开端日期
  function getLastWeekStartDate() {
    let weekLastDate = new Date(new Date(getWeekStartDate()).getTime() - 3600 * 24 * 1000 * 7);
    return formatDate(weekLastDate);
  }

  //获得上周的停止日期
  function getLastWeekEndDate() {
    let weekLastDate = new Date(new Date(getWeekStartDate()).getTime() - 3600 * 24 * 1000);
    return formatDate(weekLastDate);
  }

//获得本周的开端日期
  function getWeekStartDate() {
    let weekStartDate = new Date(nowYear, nowMonth, nowDay - nowDayOfWeek);
    return formatDate(weekStartDate);
  }

//获得本周的停止日期
  function getWeekEndDate() {
    let weekEndDate = new Date(nowYear, nowMonth, nowDay + (6 - nowDayOfWeek));
    if (new Date().getTime() < weekEndDate.getTime())return formatDate(new Date());
    return formatDate(weekEndDate);
  }

//获得本月的开端日期
  function getMonthStartDate() {
    let monthStartDate = new Date(nowYear, nowMonth, 1);
    return formatDate(monthStartDate);
  }

//获得本月的停止日期
  function getMonthEndDate() {
    let monthEndDate = new Date(nowYear, nowMonth, getMonthDays(nowMonth));
    if (new Date().getTime() < monthEndDate.getTime())return formatDate(new Date());
    return formatDate(monthEndDate);
  }

//获得上月开端时候
  function getLastMonthStartDate() {
    let lastMonthStartDate = new Date(nowYear, lastMonth, 1);
    return formatDate(lastMonthStartDate);
  }

//获得上月停止时候
  function getLastMonthEndDate() {
    let lastMonthEndDate = new Date(nowYear, lastMonth, getMonthDays(lastMonth));
    return formatDate(lastMonthEndDate);
  }


//获得本季度的开端日期
  function getQuarterStartDate() {
    let quarterStartDate = new Date(nowYear, getQuarterStartMonth(), 1);
    return formatDate(quarterStartDate);
  }

//获得本季度的停止日期
  function getQuarterEndDate() {
    let quarterEndMonth = getQuarterStartMonth() + 2;
    let quarterStartDate = new Date(nowYear, quarterEndMonth, getMonthDays(quarterEndMonth));
    if (new Date().getTime() < quarterStartDate.getTime())return formatDate(new Date());
    return formatDate(quarterStartDate);
  }

//获得上季度的开端日期
  function getLastQuarterStartDate() {
    let quarterStartDate = new Date(nowYear, getQuarterStartMonth() - 3, 1);
    return formatDate(quarterStartDate);
  }

  //获得上季度的停止日期
  function getLastQuarterEndDate() {
    let quarterEndMonth = new Date(new Date(getQuarterStartDate()).getTime() - 3600 * 24 * 1000);
    return formatDate(quarterEndMonth);
  }

  return {
    getLastWeekStartDate: getLastWeekStartDate(),
    getLastWeekEndDate: getLastWeekEndDate(),
    getWeekStartDate: getWeekStartDate(),
    getWeekEndDate: getWeekEndDate(),
    getMonthStartDate: getMonthStartDate(),
    getMonthEndDate: getMonthEndDate(),
    getLastMonthStartDate: getLastMonthStartDate(),
    getLastMonthEndDate: getLastMonthEndDate(),
    getQuarterStartDate: getQuarterStartDate(),
    getQuarterEndDate: getQuarterEndDate(),
    getLastQuarterStartDate: getLastQuarterStartDate(),
    getLastQuarterEndDate: getLastQuarterEndDate()
  }
}
export function timeDimension(a) {
  let b, c;
  let now = new Date();                    //当前日期
  let nowDayOfWeek = now.getDay();         //今天本周的第几天
  let nowDay = now.getDate();              //当前日
  let nowMonth = now.getMonth();           //当前月
  let nowYear = now.getYear();             //当前年
  nowYear += (nowYear < 2000) ? 1900 : 0;
  let lastMonthDate = new Date();  //上月日期
  lastMonthDate.setDate(1);
  lastMonthDate.setMonth(lastMonthDate.getMonth() - 1);
  let lastMonth = lastMonthDate.getMonth();
  //格式化日期：yyyy-MM-dd
  function formatDate(date) {
    let myyear = date.getFullYear();
    let mymonth = date.getMonth() + 1;
    let myweekday = date.getDate();
    if (mymonth < 10) {
      mymonth = "0" + mymonth;
    }
    if (myweekday < 10) {
      myweekday = "0" + myweekday;
    }
    return (myyear + "-" + mymonth + "-" + myweekday);
  }

  //获得某月的天数
  function getMonthDays(myMonth) {
    let monthStartDate = new Date(nowYear, myMonth, 1);
    let monthEndDate = new Date(nowYear, myMonth + 1, 1);
    let days = (monthEndDate - monthStartDate) / (1000 * 60 * 60 * 24);
    return days;
  }

  //获得昨天的开始日期
  let getUpYesterdayDate = formatDate(new Date(now - 24 * 60 * 60 * 1000));
  //获得本周的开始日期
  let getUpThisWeekStartDate = formatDate(new Date(now - (nowDayOfWeek - 1) * 86400000));
  //获得本周的结束日期
  let getUpThisWeekEndDate = formatDate(new Date((new Date(now - (nowDayOfWeek - 1) * 86400000) / 1000 + 6 * 86400) * 1000));
  //获得上周的开始日期
  let getUpLastWeekStartDate = formatDate(new Date(nowYear, nowMonth, nowDay - nowDayOfWeek - 6));
  //获得上周的结束日期
  let getUpLastWeekEndDate = formatDate(new Date(nowYear, nowMonth, nowDay - nowDayOfWeek));
  //获得下周的开始日期
  let getUpNextWeekStartDate = formatDate(new Date(nowYear, nowMonth, nowDay - nowDayOfWeek + 8));
  //获得下周的结束日期
  let getUpNextWeekEndDate = formatDate(new Date(nowYear, nowMonth, nowDay + (7 - nowDayOfWeek + 7)));
  //获得本月的开始日期
  let getUpThisMonthStartDate = formatDate(new Date(nowYear, nowMonth, 1));
  //获得本月的结束日期
  let getUpThisMonthEndDate = formatDate(new Date(nowYear, nowMonth, getMonthDays(nowMonth)));
  //获得上月开始时间
  let getLastMonthStartDate = formatDate(new Date(nowYear, lastMonth, 1));
  //获得上月结束时间
  let getLastMonthEndDate = formatDate(new Date(nowYear, lastMonth, getMonthDays(lastMonth)));
  //获得上年开始时间
  let getLastYearStartDate = nowYear - 1 + "-01-01";
  //获得上年结束时间
  let getLastYearEndDate = nowYear - 1 + "-12-31";
  switch (a) {
    case '今日':
      b = formatDate(new Date());
      c = formatDate(new Date());
      return [b, c];
      break;
    case '昨日':
      b = getUpYesterdayDate;
      c = getUpYesterdayDate;
      return [b, c];
      break;
    case '本周':
      b = getUpThisWeekStartDate;
      c = getUpThisWeekEndDate;
      return [b, c];
      break;
    case '上周':
      b = getUpLastWeekStartDate;
      c = getUpLastWeekEndDate;
      return [b, c];
      break;
    case '下周':
      b = getUpNextWeekStartDate;
      c = getUpNextWeekEndDate;
      return [b, c];
      break;
    case '本月':
      b = getUpThisMonthStartDate;
      c = getUpThisMonthEndDate;
      return [b, c];
      break;
    case '上月':
      b = getLastMonthStartDate;
      c = getLastMonthEndDate;
      return [b, c];
      break;
  }
}
//获取uuid
function guid() {
  return 'xxxxxxxx-xxxx-4xxx-yxxx-xxxxxxxxxxxx'.replace(/[xy]/g, function (c) {
    var r = Math.random() * 16 | 0, v = c == 'x' ? r : (r & 0x3 | 0x8);
    return v.toString(16);
  });
}

// 字典信息
const diction = {
  gender: [{
    label: '男',
    value: 1
  }, {
    label: '女',
    value: 2
  }],
  options: [{
    value: 1,
    label: '普通部门'
  }, {
    value: 2,
    label: '营运中心'
  }, {
    value: 3,
    label: '区域'
  }, {
    value: 4,
    label: '门店'
  }, {
    value: 5,
    label: '仓库'
  }],
  uses: [{
    label: '全部',
    value: -1
  }, {
    label: '启用',
    value: 1
  }, {
    label: '未启用',
    value: 0
  }],
  _getTimeStampfunction,
  guid,
  formatDate,
  getWeekOrMonth: getWeekOrMonth()
};
// export function timeSlot(a) {
//   switch (a) {
//     case i18n.translate('今日'):
//       return [0, i18n.translate('与昨日环比')];
//       break;
//     case i18n.translate('昨日'):
//       return [1, i18n.translate('与上周同比')];
//       break;
//     case i18n.translate('本周'):
//       return [2, i18n.translate('与上周环比')];
//       break;
//     case i18n.translate('上周'):
//       return [3, i18n.translate('与去年的同比')];
//       break;
//     /*  case '下周':
//      return 1;
//      break;*/
//     case i18n.translate('本月'):
//       return [4, i18n.translate('与上月环比')];
//       break;
//     case i18n.translate('上月'):
//       return [5, i18n.translate('与去年的同比')];
//       break;
//   }
// }
/*客流*/
// export function activeChangeIndex(a) {
//   switch (a) {
//     case '0':
//       return [i18n.translate('人'), i18n.translate('进店客流'), 'flowCount'];
//       break;
//     case '1':
//       return [i18n.translate('元'), i18n.translate('销售额'), 'salesAmount'];
//       break;
//     case '2':
//       return ['%', i18n.translate('进店率'), 'inFlowRate'];
//       break;
//     case '3':
//       return ['%', i18n.translate('转化率'), 'closeRate'];
//       break;
//   }
// }
/*保留两位小数 */
export function dataRatePercent(value) {
  if (value < 0) {
    return -parseFloat((value * 100).toFixed(2))
  }
  return parseFloat((value * 100).toFixed(2))
}

export function DateSplit(begin, end) {
  var ab = begin.split("-");
  var ae = end.split("-");
  var db = new Date();
  db.setFullYear(ab[0], ab[1] - 1, ab[2]);
  var de = new Date();
  de.setFullYear(ae[0], ae[1] - 1, ae[2]);
  var a = [];
  for (var i = 0, temp = db; temp < de; i++) {
    a[i] = GetDate(temp);
    temp.setTime(temp.getTime() + 24 * 60 * 60 * 1000);
  }
  a[i] = GetDate(de);
  return a;
}
function GetDate(d) {
  return d.getFullYear() + "-" + (d.getMonth() + 1 < 10 ? "0" + (d.getMonth() + 1) : d.getMonth() + 1) + "-" + (d.getDate() < 10 ? "0" + d.getDate() : d.getDate());
}
export function assetState(value) {
    switch (value) {
        case 1:
            return '闲置中';
            break;
        case 2:
            return '使用中';
            break;
        case 3:
            return '借用中';
            break;
        case 4:
            return '已报废';
            break;
        case 5:
            return '已禁用';
            break;
        case 6:
            return '流程中';
            break;
    }
}
export function operatingResult(value) {
    switch (value) {
        case 1:
            return '流程中';
            break;
        case 2:
            return '确认';
            break;
        case 3:
            return '退回';
            break;
        case 4:
            return '取消';
            break;
    }
}
export function discoveryState(value) {
    switch (value) {
        case 1:
            return '正常';
            break;
        case 2:
            return '盘亏';
            break;
        case 3:
            return '盘盈';
            break;
        case 4:
            return '业务流转';
            break;
    }
}
export function statePlan1(value) {
    switch (value) {
        case 1:
            return '未执行';
            break;
        case 2:
            return '待提交';
            break;
        case 3:
            return '己提交';
            break;
        case 4:
            return '已确认';
            break;
    }
}
/*某一天添加几个月*/
export function addMoth(d,m){
    var ds=d.split('-');
    d=new Date( ds[0],ds[1]-1+m,ds[2])
    return d.toLocaleDateString().match(/\d+/g).join('-')
}
/*钱的格式*/
export function formatMoney(s) {
    s = parseFloat((s + "").replace(/[^\d\.-]/g, "")).toFixed(0) + "";
    let l = s.split(".")[0].split("").reverse(),
        t = "";
    for (let i = 0; i < l.length; i++) {
        t += l[i] + ((i + 1) % 3 == 0 && (i + 1) != l.length ? "," : "");
    }
    return t.split("").reverse().join("");
}
export default diction
